package org.geometerplus.zlibrary.ui.android.view;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.LinearGradient;
import android.graphics.Paint;
import android.graphics.PaintFlagsDrawFilter;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.Shader;
import android.graphics.Typeface;
import android.graphics.drawable.GradientDrawable;
import android.text.TextPaint;
import android.view.MotionEvent;

import com.chineseall.reader.ui.util.GlobalApp;
import com.chineseall.reader.ui.util.ReadSettingUtil;
import com.chineseall.reader.ui.view.readmenu.ReadStyle;
import com.chineseall.reader.ui.view.readmenu.ReadStyleManager;
import com.chineseall.singlebook.R;
import com.iwanvi.common.utils.AndroidUtils;
import com.iwanvi.common.utils.LogUtil;

import org.w3c.dom.Text;

public class ChapterVipManager {

    private int mWidth, mHeight;
    private int mShadowHeight = (int)AndroidUtils.dip2px(GlobalApp.getInstance(), 80);;
    private ReadStyle style = ReadStyle.CLASSIC;
    private int top = 0;
    private String mPrice = "0.0";
    private String  mRemaining = "0.0";
    private int bottomHeight = (int)AndroidUtils.dip2px(GlobalApp.getInstance(), 60);
    private int buttonHeight = (int)AndroidUtils.dip2px(GlobalApp.getInstance(), 48);
    private int marginLeft = (int)AndroidUtils.dip2px(GlobalApp.getInstance(), 20);;
    private int marginRight = (int)AndroidUtils.dip2px(GlobalApp.getInstance(), 20);
    private int contentHeight = (int)AndroidUtils.dip2px(GlobalApp.getInstance(), 320);
    private int space = (int)AndroidUtils.dip2px(GlobalApp.getInstance(), 20);;
    private static ChapterVipManager chapterVipManager = null;

    private int[] text_colors_1 = {0xFF555555, 0xFF555555, 0xFF4A221D, 0xFF184F3B, 0xFF133755, 0xFF4A321D, 0xFF184F3B};
    private int[] text_colors_2 = {0xFF004A8D, 0xFF004A8D,0xFFB31717, 0xFF2F9570, 0xFF0D65A8, 0xFFAF5700, 0xFF2F9570};
    private int[] bg_colors = {0xFF18191A, 0xFFFFFFFF,0xFFF3D5D5, 0xFFDEF6DE, 0xFFD1E6F2, 0xFFF8E0BC, 0xFFDEF6DE};
    private int[] bg_shadow_colors = {0x0018191A, 0x00FFFFFF,0x00F3D5D5, 0x00DEF6DE, 0x00D1E6F2, 0x00F8E0BC, 0x00DEF6DE};
    private int[] vip_logos = {R.drawable.icon_vip_logo_black, R.drawable.icon_vip_logo_light};
    private static Bitmap bmp_logo = null;

    private Rect btnArea = new Rect(0,0,0,0);
    private Rect txtArea1 = new Rect(0,0,0,0);
    private Rect txtArea2 = new Rect(0,0,0,0);

    private int chapterState = 0;

    public static ChapterVipManager getInstance(){
        if(null == chapterVipManager){
            chapterVipManager = new ChapterVipManager();
            chapterVipManager.chapterState = 0;
        }
        if((null == bmp_logo) || (chapterVipManager.style !=ReadSettingUtil.getReadStyle())) {
            if((null != bmp_logo) && (!bmp_logo.isRecycled())){
                bmp_logo.recycle();
                bmp_logo = null;
            }
            chapterVipManager.style = ReadSettingUtil.getReadStyle();
            if(chapterVipManager.style == ReadStyle.NIGHT) {
                chapterVipManager.bmp_logo = BitmapFactory.decodeResource(GlobalApp.getInstance().getResources(), chapterVipManager.vip_logos[0]);
            }else{
                chapterVipManager.bmp_logo = BitmapFactory.decodeResource(GlobalApp.getInstance().getResources(), chapterVipManager.vip_logos[1]);
            }
        }


        return chapterVipManager;
    }

    public void drawVip(Canvas canvas){
        if(null == canvas){
            return ;
        }
        mWidth = canvas.getWidth();
        mHeight = canvas.getHeight();
        top = mHeight-contentHeight;

        canvas.setDrawFilter(new PaintFlagsDrawFilter(0,Paint.ANTI_ALIAS_FLAG | Paint.FILTER_BITMAP_FLAG));

        drawTopShadow(canvas);
        drawBackground(canvas);
        drawPrice(canvas);
        drawButton(canvas);
        drawDivider(canvas);
        drawVipWarn(canvas);
    }

    /**
     * 绘制顶部半透明阴影
     * @param canvas
     */
    private void drawTopShadow(Canvas canvas){
        ReadStyle style = ReadSettingUtil.getReadStyle();
        Paint paint=new Paint();  //定义一个Paint
        int color = ReadStyleManager.getResourceIdByStyle(bg_colors, style);
        int shadowcolor = ReadStyleManager.getResourceIdByStyle(bg_shadow_colors, style);
        GradientDrawable mShadowDrawable = new GradientDrawable(
                GradientDrawable.Orientation.TOP_BOTTOM, new int[]{shadowcolor, color});
        mShadowDrawable.setBounds(0, top, mWidth, top+mShadowHeight);
        mShadowDrawable.draw(canvas);
    }

    /**
     * 绘制内容区背景
     * @param canvas
     */
    private void drawBackground(Canvas canvas){
        ReadStyle style = ReadSettingUtil.getReadStyle();
        Paint mPaint = new Paint();
        mPaint.setStyle(Paint.Style.FILL_AND_STROKE);
        mPaint.setStrokeWidth(1);
        int color = ReadStyleManager.getResourceIdByStyle(bg_colors, style);
        mPaint.setColor(color);
        canvas.drawRect(0, top+mShadowHeight, mWidth, mHeight, mPaint);
    }
    private void drawPrice(Canvas canvas){
        int textSize = AndroidUtils.sp2px(GlobalApp.getInstance(), 15);
        TextPaint textPaint = getTextPaint(textSize, text_colors_1);
        TextPaint textPaint2 = getTextPaint(textSize, text_colors_2);

        String str = "价格:  "+mPrice+"  铜币  "+"|"+"  余额:  "+mRemaining+"  铜币";
        int textLength = (int)textPaint.measureText(str);
        int offsetX = marginLeft+(mWidth-marginLeft-marginRight-textLength)/2;
        int y = top+mShadowHeight+(int)AndroidUtils.dip2px(GlobalApp.getInstance(), 30);

        str = "价格:  ";
        canvas.drawText(str, offsetX, y, textPaint);
        offsetX += textPaint.measureText(str);

        str = mPrice;
        canvas.drawText(str, offsetX, y, textPaint2);
        offsetX += textPaint2.measureText(str);

        str = "  铜币  ";
        canvas.drawText(str, offsetX, y, textPaint);
        offsetX += textPaint.measureText(str);

        str = "|";
        canvas.drawText(str, offsetX, y, textPaint2);
        offsetX += textPaint2.measureText(str);

        str = "  余额:  ";
        canvas.drawText(str, offsetX, y, textPaint);
        offsetX += textPaint.measureText(str);

        str = mRemaining;
        canvas.drawText(str, offsetX, y, textPaint2);
        offsetX += textPaint2.measureText(str);

        str = "  铜币";
        canvas.drawText(str, offsetX, y, textPaint);
    }

    private TextPaint getTextPaint(int size, int[] colors) {
        TextPaint textPaint = new TextPaint(Paint.ANTI_ALIAS_FLAG | Paint.DEV_KERN_TEXT_FLAG);// 设置画笔
        /*textPaint.setTextAlign(Paint.Align.CENTER);*/
        textPaint.setTextSize(size);
        textPaint.setColor(ReadStyleManager.getResourceIdByStyle(colors, style));
        return textPaint;
    }

    private void drawButton(Canvas canvas){
        int textSize = AndroidUtils.sp2px(GlobalApp.getInstance(), 16);
        TextPaint textPaint = getTextPaint(textSize, text_colors_2);

        String str = "购买本章";
        int startX = marginLeft;
        int startY = top+mShadowHeight+(top-mShadowHeight-space-bottomHeight-buttonHeight)/2;
        int endX = mWidth-marginRight;
        int endY = startY+buttonHeight;
        btnArea.left = startX;
        btnArea.right = endX;
        btnArea.top = startY;
        btnArea.bottom = endY;
        RectF rectF = new RectF(startX, startY, endX, endY);
        Paint mPaint = new Paint();
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setStrokeWidth(2);
        mPaint.setColor(ReadStyleManager.getResourceIdByStyle(text_colors_2, style));
        canvas.drawRoundRect(rectF, buttonHeight, buttonHeight, mPaint);
        TextPaint.FontMetrics fontMetrics = textPaint.getFontMetrics();
        float textLength = textPaint.measureText(str);

        startX = (int)(startX+(rectF.width()-textLength)/2);
        int y = startY+(int)fontMetrics.descent+(int)buttonHeight/2;
        textPaint.setTypeface(Typeface.DEFAULT_BOLD);
        canvas.drawText(str, startX, y, textPaint);

        str = "批量购买，享受更多优惠 >";
        textSize = AndroidUtils.sp2px(GlobalApp.getInstance(), 12);
        textPaint.setTextSize(textSize);
        textPaint.setTypeface(Typeface.DEFAULT);
        textLength = textPaint.measureText(str);
        startX = (int)(mWidth-textLength)/2;
        txtArea1.top = startY+buttonHeight+space;
        startY = startY+buttonHeight+space+(int)(fontMetrics.bottom-fontMetrics.top)/2;
        canvas.drawText(str, startX, startY, textPaint);

        txtArea1.left = startX;
        txtArea1.right = startX+(int)textLength;
        txtArea1.bottom =mHeight-bottomHeight;
    }

    /**
     * 绘制分割线
     * @param canvas
     */
    private void drawDivider(Canvas canvas){
        Paint mPaint = new Paint();
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setStrokeWidth(2);
        int color = GlobalApp.getInstance().getResources().getColor(ReadStyleManager.getResourceIdByStyle(ReadStyleManager.bookmark_chapter_divider_colors, style));
        mPaint.setColor(color);
        int y = mHeight-bottomHeight;
        canvas.drawLine(0, y,mWidth, y, mPaint);
    }

    /**
     *绘制vip提示信息
     * @param canvas
     */
    private void drawVipWarn(Canvas canvas){

        int textSize = AndroidUtils.sp2px(GlobalApp.getInstance(), 12);
        TextPaint textPaint = getTextPaint(textSize, text_colors_2);
        TextPaint.FontMetrics fontMetrics = textPaint.getFontMetrics();
        String str = "  开通VIP，立享全站免费 >";
        int x = 0;
        int centY = mHeight-bottomHeight/2;;
        float textLength = textPaint.measureText(str);
        if(null != bmp_logo){
            x = marginLeft+(int)(mWidth-marginRight-marginLeft-bmp_logo.getWidth()-textLength)/2;
            int y = centY - bmp_logo.getHeight()/2;
            canvas.drawBitmap(bmp_logo, x, y, null);
            y = centY + (int)fontMetrics.descent;
            canvas.drawText(str, x+bmp_logo.getWidth(), y, textPaint);
            txtArea2.left = x;
            txtArea2.right = x+bmp_logo.getWidth()+(int)textLength;
            txtArea2.top = mHeight-bottomHeight;
            txtArea2.bottom = mHeight;
        }else{
            x = (int)(mWidth-marginRight-marginLeft-textLength)/2;
            canvas.drawText(str, x+bmp_logo.getWidth(), centY+fontMetrics.descent, textPaint);
            txtArea2.left = x;
            txtArea2.right = x+(int)textLength;
            txtArea2.top = mHeight-bottomHeight;
            txtArea2.bottom = mHeight;
        }
    }

    public void setChapterState(int chapterState) {
        this.chapterState = chapterState;
    }

    public int getChapterState() {
        return chapterState;
    }

    public boolean onFingerSingleTap(int x, int y) {
        if(0 == chapterState){
            //处理点击事件
            LogUtil.i("zhl-6", "txtArea1.left = "+txtArea1.left+"; txtArea1.right = "+txtArea1.right+"; txtArea1.top = "+txtArea1.top+"; txtArea1.bottom = "+txtArea1.bottom+"; x = "+x+"; y = "+y);
            if((x > btnArea.left) && (x <btnArea.right) && (y > btnArea.top) && (y < btnArea.bottom)){
                LogUtil.i("zhl-6", "btnArea.left = "+btnArea.left+"; btnArea.right = "+btnArea.right+"; btnArea.top = "+btnArea.top+"; btnArea.bottom = "+btnArea.bottom+ "; x = "+x+"; y = "+y);
                onBtnClick();
            }else if ((x > txtArea1.left) && (x < txtArea1.right) && (y > txtArea1.top) && (y < txtArea1.bottom)){
                LogUtil.i("zhl-6", "txtArea1.left = "+txtArea1.left+"; txtArea1.right = "+txtArea1.right+"; txtArea1.top = "+txtArea1.top+"; txtArea1.bottom = "+txtArea1.bottom+"; x = "+x+"; y = "+y);
                onTxt1Click();
            }else if ((x > txtArea2.left) && (x <txtArea2.right) && (y > txtArea2.top) && (y < txtArea2.bottom)){
                LogUtil.i("zhl-6", "txtArea2.left = "+txtArea2.left+"; txtArea2.right = "+txtArea2.right+"; txtArea2.top = "+txtArea2.top+"; txtArea2.bottom = "+txtArea2.bottom+"; x = "+x+"; y = "+y);
                onTxt2Click();
            }else{
                return false;
            }
            return true;
        }

        return false;
    }

    private void onBtnClick(){
        LogUtil.w("zhl-6", "###############onBtnClick");
    }

    private void onTxt1Click(){
        LogUtil.w("zhl-6", "###############onTxt1Click");
    }

    private void onTxt2Click(){
        LogUtil.w("zhl-6", "###############onTxt2Click");
    }
}
